package com.yzt.dynamicmodule.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

import com.yzt.dynamicmodule.R;

import cn.jzvd.JZUtils;
import cn.jzvd.JzvdStd;

/**
 * @author jiucheng
 * @描述: 动态的视频播放器
 */
public class DynamicVideoView extends JzvdStd {

    private TextView mDynamicTv;

    private OnScreenClickListener screenClickListener;

    private boolean needVolume = true;

    public DynamicVideoView(Context context) {
        super(context);
    }

    public void setOnScreenClickListener(OnScreenClickListener screenClickListener) {
        this.screenClickListener = screenClickListener;
    }

    public DynamicVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setUp(String url, String title, int screen) {
        if (!TextUtils.isEmpty(url)) {
            url = url.replace("http://", "https://");
        }
        super.setUp(url, title, screen);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        reset();
    }

    @Override
    public void init(Context context) {
        super.init(context);
        mDynamicTv = findViewById(R.id.tv_dynamic_time);
        topContainer.setVisibility(GONE);
        bottomContainer.setVisibility(GONE);
    }

    public void setVolume() {
        needVolume = false;
    }

    @Override
    public void onClickUiToggle() {
        super.onClickUiToggle();
        screenClickListener.onScreenClick();
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_dynamic_video_view;
    }

    @Override
    public void setAllControlsVisiblity(int topCon, int bottomCon, int startBtn, int loadingPro, int posterImg, int bottomPro, int retryLayout) {
        super.setAllControlsVisiblity(topCon, bottomCon, startBtn, loadingPro, posterImg, bottomPro, retryLayout);
        topContainer.setVisibility(GONE);
        bottomContainer.setVisibility(GONE);
    }

    @Override
    public void onProgress(int progress, long position, long duration) {
        if (!needVolume && state == STATE_PLAYING && mediaInterface != null) {
            mediaInterface.setVolume(0f, 0f);
        }
        super.onProgress(progress, position, duration);
        if (position != 0) {
            mDynamicTv.setText(JZUtils.stringForTime(position));
        }
    }

    @Override
    public void onPrepared() {
        if (!needVolume && state == STATE_PLAYING && mediaInterface != null) {
            mediaInterface.setVolume(0f, 0f);
        }
        super.onPrepared();
    }

    public interface OnScreenClickListener {

        void onScreenClick();
    }
}
